﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.DTO;
using Restaurant.BUS;

namespace Restaurant
{
    public partial class frmNhapHang : Form
    {
        private ChiTietKhoService CTKhoService = new ChiTietKhoService();
        private DataTable dbNguyenLieu = new DataTable();
        private DataTable dbNguyenLieuNhap = GetTableNguyenLieuNhap();
        private NhapHangService nhService = new NhapHangService();
        private int MaKho;

        public void LoadNguyenLieu(DataTable dbtab)
        {
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow["MaNL"].ToString();
                liv.SubItems.Add(dbrow["TenNL"].ToString());
                //liv.SubItems.Add(dbrow[2].ToString());
                liv.SubItems.Add(dbrow["TenLoaiNL"].ToString());
                liv.SubItems.Add(dbrow["TenDVTinh"].ToString());
                liv.SubItems.Add(dbrow["LuongTon"].ToString());
                lv_NL.Items.Add(liv);
            }
        }
        public void LoadNguyenLieuNhap(DataTable dbtab)
        {
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                liv.SubItems.Add(dbrow[2].ToString());
                liv.SubItems.Add(dbrow[3].ToString());
                liv.SubItems.Add(dbrow[4].ToString());
                liv.SubItems.Add(dbrow[5].ToString());
                liv.SubItems.Add(dbrow[6].ToString());
                lv_NLNhap.Items.Add(liv);
            }
        }

        public frmNhapHang()
        {
            InitializeComponent();
        }
        public frmNhapHang(int maKho)
        {
            MaKho = maKho;
            InitializeComponent();
        }
        private void LoadNCC()
        {
            NhaCungCapService NCC = new NhaCungCapService();
            cmbNCC.DisplayMember = "TenNCC";
            cmbNCC.ValueMember = "MaNCC";
            cmbNCC.DataSource = NCC.LoadTatCaNhaCungCap();
        }
        private void LoadHTTToan()
        {
            HanThanhToanService htt = new HanThanhToanService();
            cmbHTTToan.DisplayMember = "TenHTThanhToan";
            cmbHTTToan.ValueMember = "MaHTThanhToan";
            cmbHTTToan.DataSource = htt.LoadHinhThucThanhToan();
        }

        private void cmbNCC_SelectedIndexChanged(object sender, EventArgs e)
        {
            int maNCC = int.Parse(cmbNCC.SelectedValue.ToString());
            dbNguyenLieu = CTKhoService.LayNguyenLieuCTKhoNCC(MaKho, maNCC);
            lv_NL.Items.Clear();
            LoadNguyenLieu(dbNguyenLieu);
            //cmbNCC.Enabled = false;
        }

        private void frmQLNhapHang_Load(object sender, EventArgs e)
        {
            LoadNCC();
            LoadHTTToan();
            cmbTinhTrang.SelectedIndex = 0;
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            //cmbNCC.Enabled = true;
            dbNguyenLieu = CTKhoService.LayChiTietKho(MaKho);
            lv_NL.Items.Clear();
            LoadNguyenLieu(dbNguyenLieu);
            lv_NLNhap.Items.Clear();
            dbNguyenLieuNhap = GetTableNguyenLieuNhap();
            txtTongTien.Text = "";
            txtSoLuong.Text = "";
            txtDonGia.Text = "";
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            
            if (lv_NL.FocusedItem != null)
            {
                
                //ListViewItem liv = new ListViewItem();
                //liv.Text = lv_NL.FocusedItem.SubItems[0].Text;
                //liv.SubItems.Add(lv_NL.FocusedItem.SubItems[1].Text);
                //liv.SubItems.Add(lv_NL.FocusedItem.SubItems[2].Text);
                //liv.SubItems.Add(lv_NL.FocusedItem.SubItems[3].Text);
                //liv.SubItems.Add(txtSoLuong.Text.ToString());
                //liv.SubItems.Add(txtDonGia.Text.ToString());
                //liv.SubItems.Add(( DonGia * SoLuong).ToString());
                //lv_NLNhap.Items.Add(liv);
                //lv_NL.Items[lv_NL.FocusedItem.Index].SubItems.Clear();

                //Cap nhat LV Nguyen Lieu Kho
                lv_NLNhap.Items.Clear();
                if (txtDonGia.Text == "")
                    return;
                if (txtSoLuong.Text == "")
                    return;
                int DonGia = int.Parse(txtDonGia.Text);
                float SoLuong = float.Parse(txtSoLuong.Text);
                int maNL = int.Parse(lv_NL.FocusedItem.Text);
                dbNguyenLieuNhap.Rows.Add(lv_NL.FocusedItem.SubItems[0].Text, lv_NL.FocusedItem.SubItems[1].Text, lv_NL.FocusedItem.SubItems[2].Text, lv_NL.FocusedItem.SubItems[3].Text, SoLuong, DonGia, SoLuong * DonGia);
                LoadNguyenLieuNhap(dbNguyenLieuNhap);

                //cap nhat LV Nguyen Lieu
                for (int i = 0; i < dbNguyenLieu.Rows.Count; i++)
                {
                    if (int.Parse(dbNguyenLieu.Rows[i][0].ToString()) == maNL)
                        dbNguyenLieu.Rows.Remove(dbNguyenLieu.Rows[i]); 
                }
                lv_NL.Items.Clear();
                LoadNguyenLieu(dbNguyenLieu);

                //Tinh Tong Tien
                int TongTien = 0;
                for (int i = 0; i < dbNguyenLieuNhap.Rows.Count; i++)
                {
                    TongTien += int.Parse(dbNguyenLieuNhap.Rows[i]["ThanhTien"].ToString());
                }
                txtTongTien.Text = TongTien.ToString();
            }
            else
            {
                MessageBox.Show("Chưa chọn nguyên liệu");
            }
        }
        private static DataTable GetTableNguyenLieuNhap()
        {
            // Here we create a DataTable with four columns.
            DataTable table = new DataTable();
            table.Columns.Add("MaNL", typeof(int));
            table.Columns.Add("TenNL", typeof(string));
            table.Columns.Add("TenLoaiNL", typeof(string));
            table.Columns.Add("TenDVTinh", typeof(string));
            table.Columns.Add("SoLuong", typeof(float));
            table.Columns.Add("DonGia", typeof(long));
            table.Columns.Add("ThanhTien", typeof(long));
            return table;
        }

        private void btnUnMove_Click(object sender, EventArgs e)
        {
            if (lv_NLNhap.FocusedItem != null)
            {
                int maNL = int.Parse(lv_NLNhap.FocusedItem.Text);
                //Cap nhat LV Nguyen Lieu
                dbNguyenLieu.Rows.Add(lv_NLNhap.FocusedItem.SubItems[0].Text, lv_NLNhap.FocusedItem.SubItems[1].Text,null, lv_NLNhap.FocusedItem.SubItems[2].Text, lv_NLNhap.FocusedItem.SubItems[3].Text);
                lv_NL.Items.Clear();
                LoadNguyenLieu(dbNguyenLieu);

                //Cap nhat LV Nguyen Lieu Nhap
                for (int i = 0; i < dbNguyenLieuNhap.Rows.Count; i++)
                {
                    if (int.Parse(dbNguyenLieuNhap.Rows[i]["MaNL"].ToString()) == maNL)
                        dbNguyenLieuNhap.Rows.Remove(dbNguyenLieuNhap.Rows[i]);
                }
                lv_NLNhap.Items.Clear();
                LoadNguyenLieuNhap(dbNguyenLieuNhap);
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn nguyên liệu");
            }
        }

        private void btnNhapHang_Click(object sender, EventArgs e)
        {
            if(lv_NLNhap.Items.Count > 0)
            {
                NhapHang nh = new NhapHang();
                nh.MANHAPHANG = nhService.TaoMaNhapHang();
                nh.CHITIETNHAP = dbNguyenLieuNhap;
                nh.MAHTTOAN = int.Parse(cmbHTTToan.SelectedValue.ToString());
                nh.MAKHO = MaKho;
                nh.MANCC = int.Parse(cmbNCC.SelectedValue.ToString());
                nh.NGAYNHAP = dtpNgayNhap.Value;
                nh.NGAYTHANHTOAN = dtpNgayThanhToan.Value;
                nh.TINHTRANG = int.Parse(cmbTinhTrang.SelectedIndex.ToString());
                nh.TONGTIEN = float.Parse(txtTongTien.Text);

                //Nhap hang + cap nhat luong ton chitietkho
                if (!fix && !deadlock)
                {
                    if (!nhService.ThemNhapHang(nh))
                    {
                        MessageBox.Show("Nhập hàng không thành công!");
                        return;
                    }
                    else
                    {
                        if (nhService.ThemCTNhapHang(nh))
                        {
                            //them chi tiet nhap hang thanh cong
                            dbNguyenLieuNhap = GetTableNguyenLieuNhap();
                            dbNguyenLieuNhap.Rows.Clear();
                            lv_NLNhap.Items.Clear();
                            dbNguyenLieu = CTKhoService.LayChiTietKho(MaKho);
                            lv_NL.Items.Clear();
                            LoadNguyenLieu(dbNguyenLieu);
                        }
                        else
                        {
                            //them khong thanh cong. Xoa NhapHang.
                            nhService.XoaNhapHang(nh.MANHAPHANG);
                            MessageBox.Show("Nhập hàng không thành công");
                        }
                    }
                }
                if (!fix && deadlock)
                {
                    if (!nhService.ThemNhapHang(nh))
                    {
                        MessageBox.Show("Nhập hàng không thành công!");
                        return;
                    }
                    else
                    {
                        if (nhService.ThemCTNhapHangDeadLockError(nh))
                        {
                            //them chi tiet nhap hang thanh cong
                            dbNguyenLieuNhap = GetTableNguyenLieuNhap();
                            dbNguyenLieuNhap.Rows.Clear();
                            lv_NLNhap.Items.Clear();
                            dbNguyenLieu = CTKhoService.LayChiTietKho(MaKho);
                            lv_NL.Items.Clear();
                            LoadNguyenLieu(dbNguyenLieu);
                        }
                        else
                        {
                            //them khong thanh cong. Xoa NhapHang.
                            nhService.XoaNhapHang(nh.MANHAPHANG);
                            MessageBox.Show("Nhập hàng không thành công");
                        }
                    }
                }
                if (fix && deadlock)
                {
                    if (!nhService.ThemNhapHang(nh))
                    {
                        MessageBox.Show("Nhập hàng không thành công!");
                        return;
                    }
                    else
                    {
                        if (nhService.ThemCTNhapHangDeadLock(nh))
                        {
                            //them chi tiet nhap hang thanh cong
                            dbNguyenLieuNhap = GetTableNguyenLieuNhap();
                            dbNguyenLieuNhap.Rows.Clear();
                            lv_NLNhap.Items.Clear();
                            dbNguyenLieu = CTKhoService.LayChiTietKho(MaKho);
                            lv_NL.Items.Clear();
                            LoadNguyenLieu(dbNguyenLieu);
                        }
                        else
                        {
                            //them khong thanh cong. Xoa NhapHang.
                            nhService.XoaNhapHang(nh.MANHAPHANG);
                            MessageBox.Show("Nhập hàng không thành công");
                        }
                    }
                }
                
            }else
                MessageBox.Show("Không có nguyên liệu trong danh sách");
        }

        private void cmbTinhTrang_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private bool fix = false;
        private bool deadlock = false;

        private void cb_deadlock_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_deadlock.Checked)
                deadlock = true;
            else
                deadlock = false;
        }

        private void cb_fix_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_fix.Checked)
                fix = true;
            else
                fix = false;
        }
    }
}
